﻿//原HGE 例程5
/*
** Haaf's Game Engine 1.6
** Copyright (C) 2003-2006, Relish Games
** hge.relishgames.com
**
** hge_tut05 - Using distortion mesh
*/


// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.


import core.hgeall;
import std.c.math;


// Pointer to the HGE interface.
// Helper classes require this to work.

class Myclass: hgeSceneObj
{
	ifHge hge;
	// Pointers to the HGE objects we will use
	hgeDistortionMesh	dis;
	hgeFontCN			fnt;
	hgeSprite			bgspr;

	float  mousex,mousey;
	// Some "gameplay" variables
	const int nRows=16;
	const int nCols=16;
	const float cellw=512.0f/(nCols-1);
	const float cellh=512.0f/(nRows-1);

	const float meshx=144;
	const float meshy=44;
		static int trans=0;
	this(){
			super("场景1");
			hge= gameInfo.hge;
		//载入图片时,可指定关键色,将被透明处理
			HTEXTURE tex	=gameInfo.res.tex_load( r"res\colorkey_texture.png",0x00FF0000);
			HTEXTURE bgtex	=gameInfo.res.tex_load( r"res\bg2.png");

		// Load font, create sprites
			bgspr=new hgeSprite(bgtex,0,0,800,600);
			bgspr.SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
			bgspr.SetColor(0xFF000000,0);
			bgspr.SetColor(0xFF000000,1);
			bgspr.SetColor(0xFF000040,2);
			bgspr.SetColor(0xFF000040,3); 
		// Create a distortion mesh
		dis=new hgeDistortionMesh(nCols, nRows);
		dis.SetTexture(tex);
		dis.SetTextureRect(0,0,512,512);
		dis.SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_ZWRITE);
		dis.Clear(0xFF000000);

		fnt =new hgeFontCN(r"res\font_cn.sbin");

		}
		bool update()
		{
			float dt=hge.Timer_GetDelta();
			static float t=0.0f;


			int i, j, col;
			float r, a, dx, dy;

			t+=dt;

			hge.Input_GetMousePos(&mousex,&mousey);
			// Process keys
			switch(hge.Input_GetKey())
			{
				case HGEK_ESCAPE:
					return true;

				case HGEK_SPACE:
					if(++trans > 2) trans=0;
					dis.Clear(0xFF000000);
					break;
				default:	 break;
			}
			
			// Calculate new displacements and coloring for one of the three effects
			switch(trans)
			{
				case 0:	for(i=1;i<nRows-1;i++)
							for(j=1;j<nCols-1;j++)
							{
								dis.SetDisplacement(j,i,cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
							}
						break;

				case 1:	for(i=0;i<nRows;i++)
							for(j=1;j<nCols-1;j++)
							{
								dis.SetDisplacement(j,i,cosf(t*5+j/2)*15,0,HGEDISP_NODE);
								col=cast(int)((cosf(t*5+(i+j)/2)+1)*35);
								dis.SetColor(j,i,0xFF<<24 | col<<16 | col<<8 | col);
							}
						break;

				case 2:	for(i=0;i<nRows;i++)
							for(j=0;j<nCols;j++)
							{
								r=sqrtf(powf(j-cast(float)nCols/2,2)+powf(i-cast(float)nRows/2,2));
								a=r*cosf(t*2)*0.1f;
								dx=sinf(a)*(i*cellh-256)+cosf(a)*(j*cellw-256);
								dy=cosf(a)*(i*cellh-256)-sinf(a)*(j*cellw-256);
								dis.SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);
								col=cast(int)((cos(r+t*4)+1)*40);
								dis.SetColor(j,i,0xFF<<24 | col<<16 | (col/2)<<8);
							}
							break;
				default:	break;
			}
			return false;
		}

		bool draw()
		{
			// Render graphics
			hge.Gfx_Clear(0xFFFF0000);
			if(!trans)
				bgspr.Render(0,0);
			//dis.Render(meshx, meshy);
			dis.Render(mousex,mousey);
			
			fnt.printf(5, 5, HGETEXT_LEFT, 
					"dt:%.3f\nFPS:%d
					Use yourSPACE!", 	
					hge.Timer_GetDelta(), 
					hge.Timer_GetFPS());
			
			return false;
		}

}

void main()
{	
	gameInfo gi= gameInfo.getInstance();

	gi.setVideo(800,600,32);
	gi.setStr("sample2","sample2.log");

	try{
	gi.init();
	
	Myclass  myObj=new  Myclass;
	gi.run( myObj);
	
	}catch(Exception e){
		mes("主程异常:	",e);
	}finally{
		delete gi;
	}
}
